/**
 * @author Bushra Nazir
 */
package com.es.business.businessRule;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.es.bo.LeadQualificationPropertyValueBO;
import com.es.bo.ValueBO;
import com.es.dao.LeadDAO;
import com.es.dao.LeadQualificationDAO;
import com.es.dao.PropertyDAO;
import com.es.dao.ValueDAO;
import com.es.dao.factory.DAOFactory;
import com.es.exception.business.ESException;
import com.es.model.Lead;
import com.es.model.LeadQualification;
import com.es.model.Property;
import com.es.model.Value;
import com.es.util.DTOToBOConvertor;
import com.es.util.ProjectConstant;
import com.es.util.ProjectUtility;

/**
 * @author Bushra
 *
 */
public class BusinessRuleImpl implements IBusinessRuleBL{

	private static final Logger log = Logger.getLogger(BusinessRuleImpl.class);
	//private BusinessRuleDAO businessRuleDAO = (BusinessRuleDAO)DAOFactory.getDataAccessObject(DAOFactory.BUSINESS_RULE_DAO);
	private PropertyDAO propertyDAO = (PropertyDAO)DAOFactory.getDataAccessObject(DAOFactory.PROPERTY_DAO);
	private ValueDAO valueDAO = (ValueDAO)DAOFactory.getDataAccessObject(DAOFactory.VALUE_DAO);
	private LeadQualificationDAO leadQualificationDAO = (LeadQualificationDAO)DAOFactory.getDataAccessObject(DAOFactory.LEAD_QUALIFICATION_DAO);

	LeadDAO leadDAO = (LeadDAO) DAOFactory.getDataAccessObject(DAOFactory.LEAD_DAO);



	/*
	 * @author Bushra 
	 */

	public List<LeadQualificationPropertyValueBO> getUniqueLeadQualificationQuestions(
			Lead selectedLeadToConvert, Integer businessRuleId) throws ESException {


		log.info("INSIDE getUniqueLeadQualificationQuestions()" );
		List <LeadQualificationPropertyValueBO> LeadQualificationPropertyValueBOList = new ArrayList<LeadQualificationPropertyValueBO> ();


		List <Property> leadQualificationRulePropertyList = null;
		//getBusinessRuleProperties();

		List<ValueBO> valueBOList = new ArrayList<ValueBO> ();

		if(selectedLeadToConvert != null){

			leadQualificationRulePropertyList =  getUniqueLeadQualificationPropertiesByUser (selectedLeadToConvert, businessRuleId);

		} else{
			log.info("Lead is null");
		}

		log.info(leadQualificationRulePropertyList.size());


		for (int i=0; i<leadQualificationRulePropertyList.size(); i++){

			List <LeadQualification> leadQualificationList = new ArrayList<LeadQualification> ();
			LeadQualificationPropertyValueBOList.add(new LeadQualificationPropertyValueBO(leadQualificationRulePropertyList.get(i) ,getValueBOList(leadQualificationRulePropertyList.get(i)), leadQualificationList));

		}


		return LeadQualificationPropertyValueBOList;
	}







	/*
	 * @author Bushra 
	 */

	private List<Property> getUniqueLeadQualificationPropertiesByUser(
			Lead selectedLeadToConvert, Integer businessRuleId) {

		log.info("INSIDE getUniqueLeadQualificationPropertiesByUser()" );

		List <Property> uniqueProperties = null;
		uniqueProperties = propertyDAO.getLeadQualificationPropertyLisByUser(selectedLeadToConvert, businessRuleId  );

		return uniqueProperties;

	}





	/*
	 * @author Bushra 
	 */
	private List<ValueBO> getValueBOList(Property property) {


		log.info("INSIDE getValueBOList()" );	

		List <Value> valueList = null;
		List <ValueBO> valueBOList = null;

		valueList = valueDAO.getValues(property);	

		valueBOList = new ArrayList <ValueBO> ();

		DTOToBOConvertor.convertValueListToValueBOList(valueList, valueBOList);

		return valueBOList;

	}







	/*
	 * @author Bushra 
	 * @param List<LeadQualificationPropertyValueBO>, Lead selectedLeadToConvert
	 */

	public void saveLeadQualificationValues (
			List<LeadQualificationPropertyValueBO> leadQualificationPropertyValueBOList,
			Lead selectedLeadToConvert) throws ESException {

	
		int total_qualification_factors_true = 0;
		int min_qualification_factors_required_true = Integer.parseInt(ProjectUtility.getMessageResourceString(ProjectConstant.APPLICATION_BUSINESS_RULE_RESOURCES_PATH, ProjectConstant.MIN_QUALIFICATION_FACTORS_REQUIRED_FULFILLED_KEY));

		log.info("min_qualification_factors_required_true :: " +  min_qualification_factors_required_true);

		int selectedCheck = 0;

		if(selectedLeadToConvert!=null){

			for( int i=0; i< leadQualificationPropertyValueBOList.size(); i++){

				LeadQualificationPropertyValueBO leadQualificationPropertyValueBO =  leadQualificationPropertyValueBOList.get(i);
				List<ValueBO> valueBOList = leadQualificationPropertyValueBO.getValueBOlist();

				LeadQualification leadQualification;

				if( leadQualificationPropertyValueBO.getProperty().getType().equals("M")){

					log.info("MULTI-VALUE PROPERTY");

					for ( int j= 0; j< valueBOList.size() ; j++)   {

						ValueBO valueBO = valueBOList.get(j);

						if(valueBO.getIsselected()){

							log.info("OPTION SELECTED :: " + valueBO.getValue());

							selectedCheck +=1;

							leadQualification = new LeadQualification();
							leadQualification.setValueId(valueBO.getValueId());
							leadQualification.setLeadId(selectedLeadToConvert.getLeadId());
							leadQualification.setPropertyId(valueBO.getPropertyId());

							leadQualificationPropertyValueBO.getLeadQualificationList().add(leadQualification);

						} else {
							log.info("OPTION NOT SELECTED");
						}
					}

				} else if(leadQualificationPropertyValueBO.getProperty().equals("S")){

					log.info("SINGLE CHIOCE QUESTION");

					/*List<LeadQualification>   leadQualificationList =  leadQualificationPropertyValueBO.getLeadQualificationList();
					for ( int j= 0; j< leadQualificationList.size() ; j++)   {

						LeadQualification leadQualificationSV = leadQualificationList.get(j);
						if(leadQualificationSV.getLeadId() == null){

							leadQualificationSV.setLeadId(selectedLeadToConvert.getLeadId());
						}
					}*/

				}

				log.info("selectedCheck" + selectedCheck);
				total_qualification_factors_true +=  checkLeadQualification(leadQualificationPropertyValueBO.getLeadQualificationList());

			}

			if (total_qualification_factors_true >= min_qualification_factors_required_true){

				log.info("check :: true" );
				//leadQualificationDAO.saveLeadQualificationValueList(leadQualificationPropertyValueBO.getLeadQualificationList());
				leadQualificationDAO.saveLeadQualificationValues(leadQualificationPropertyValueBOList);

				selectedLeadToConvert.setIsqualified(ProjectConstant.LEAD_IS_QUALIFIED_CD);
			
				leadDAO.modifyLead(selectedLeadToConvert);
				//selectedLeadToConvert.setLeadId(leadId);
			
				
				
			}else{

				for (int i=0; i< leadQualificationPropertyValueBOList.size(); i++){

					List <LeadQualification> qualList = leadQualificationPropertyValueBOList.get(i).getLeadQualificationList();

					for (int j=0; j< qualList.size(); j++){

						qualList.remove(j);
					}

				}	
				throw new ESException( " lead could not qualify.. ");

			}

			

		}
		

	}







/*	private String navigateToAutoCreateContactPage() {
		
		String navigationIdentifier = ProjectConstant.AUTO_CREATE_CONTACT_NAVIGATION_INDENTIFIER;
		return navigationIdentifier;
		
	}*/







	private int checkLeadQualification(
			List<LeadQualification> leadQualificationList) {

		log.info("inside checkLeadQualification() " ) ;

		log.info(" leadQualificationList size :: " + leadQualificationList.size());

		int qualification_factors_true = 0;

		for(int i=0; i< leadQualificationList.size(); i++){

			LeadQualification leadQualification = leadQualificationList.get(i);

			log.info(leadQualification.getValueId());

			if(leadQualification.getPropertyId()== Integer.parseInt(ProjectUtility.getMessageResourceString(ProjectConstant.APPLICATION_BUSINESS_RULE_RESOURCES_PATH, ProjectConstant.QUALIFIED_TIME_PROPERTY_ID ))){

				log.info("timeframe property");

				if((leadQualification.getValueId()==Integer.parseInt(ProjectUtility.getMessageResourceString(ProjectConstant.APPLICATION_BUSINESS_RULE_RESOURCES_PATH, ProjectConstant.BR1_TIMEFRAME_QUALFICATION_KEY1))) || (leadQualification.getValueId()==Integer.parseInt(ProjectUtility.getMessageResourceString(ProjectConstant.APPLICATION_BUSINESS_RULE_RESOURCES_PATH, ProjectConstant.BR1_TIMEFRAME_QUALFICATION_KEY2)))){

					qualification_factors_true += 1; 
					log.info(" qualification_factors_true " + qualification_factors_true);

				} 

			}else if (leadQualification.getPropertyId()==Integer.parseInt(ProjectUtility.getMessageResourceString(ProjectConstant.APPLICATION_BUSINESS_RULE_RESOURCES_PATH, ProjectConstant.QUALIFIED_PRODUCT_PROPERTY_ID))){


				log.info("product-of-interest property");

				if((leadQualification.getValueId()==Integer.parseInt(ProjectUtility.getMessageResourceString(ProjectConstant.APPLICATION_BUSINESS_RULE_RESOURCES_PATH, ProjectConstant.BR1_PRODUCT_OF_INTEREST_QUALFICATION_KEY1))) || (leadQualification.getValueId()==Integer.parseInt(ProjectUtility.getMessageResourceString(ProjectConstant.APPLICATION_BUSINESS_RULE_RESOURCES_PATH, ProjectConstant.BR1_PRODUCT_OF_INTEREST_QUALFICATION_KEY2)))){

					qualification_factors_true += 1; 
					log.info(" qualification_factors_true " + qualification_factors_true);

				} 
			}else if (leadQualification.getPropertyId()==Integer.parseInt(ProjectUtility.getMessageResourceString(ProjectConstant.APPLICATION_BUSINESS_RULE_RESOURCES_PATH, ProjectConstant.QUALIFIED_DECISION_FACTOR_ID))){


				log.info("decision factor property");

				if((leadQualification.getValueId()==Integer.parseInt(ProjectUtility.getMessageResourceString(ProjectConstant.APPLICATION_BUSINESS_RULE_RESOURCES_PATH, ProjectConstant.BR1_DECISION_FACTOR_QUALFICATION_KEY1))) || (leadQualification.getValueId()==Integer.parseInt(ProjectUtility.getMessageResourceString(ProjectConstant.APPLICATION_BUSINESS_RULE_RESOURCES_PATH, ProjectConstant.BR1_DECISION_FACTOR_QUALFICATION_KEY2)))){

					qualification_factors_true += 1; 
					log.info(" qualification_factors_true " + qualification_factors_true);

				} 
			}

		}

		log.info(" qualification_factors_true :: " + qualification_factors_true);
		return qualification_factors_true;


	}




}
